package com.kl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kl.domain.LogisticsStoreDTO;
import com.kl.entity.LogisticsStore;
import com.kl.mapper.LogisticsStoreMapper;
import com.kl.service.ILogisticsStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@Service
public class LogisticsStoreServiceImpl extends ServiceImpl<LogisticsStoreMapper, LogisticsStore> implements ILogisticsStoreService {

    @Resource
    private LogisticsStoreMapper logisticsStoreMapper;



    @Override
    public Map<String,Object> selectLogisticsStoreLikePage(Integer pageNumber, Integer pageSize, String storeAddr, String storeName, String business, Integer ifWarehouse) {

        QueryWrapper<LogisticsStore> query= new QueryWrapper<>();


        query.like(StringUtils.isNoneBlank(storeAddr),"store_addr",storeAddr)
        .or().like(StringUtils.isNoneBlank(storeName),"store_name",storeName)
        .or().eq(StringUtils.isNoneBlank(business),"business",business)
        .or().eq(null !=ifWarehouse,"if_warehouse",ifWarehouse);

        //第三个参数 如为false 则不查总条数(只要数据,发送一条sql,损耗较小) 如果为true 则查总条数(发送两条sql)
        Page<LogisticsStore> page = new Page<>(pageNumber, pageSize ,true);

        IPage<LogisticsStore> iPage = logisticsStoreMapper.selectPage(page,query);

        List<LogisticsStoreDTO> list = new ArrayList<>();

        List<LogisticsStore> records = page.getRecords();
        for (LogisticsStore record : records) {
            LogisticsStoreDTO logisticsStoreDTO = new LogisticsStoreDTO();
            BeanUtils.copyProperties(record,logisticsStoreDTO);
            list.add(logisticsStoreDTO);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("总条数",iPage.getTotal());
        map.put("result",list);
        return map;
    }

    @Override
    public List<LogisticsStore> selectLogisticsStoreAll() {
        return logisticsStoreMapper.selectList(null);
    }
}
